<template>
  <div class="page">
    <div class="layui-fluid">
      <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
          <div class="layui-card">
            <div class="layui-card-body cardbody">
              <div class="layui-form" lay-filter="">


                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label">会员等级</label>
                    <div class="layui-input-inline"> 
                      <input type="text" name="levelName" placeholder="请输入会员等级" autocomplete="off" class="layui-input" disabled='disabled'
                        v-model="itemData.levelName">
                    </div>
                  </div>
                </div>

                <div class="layui-form-item">
                  <label class="layui-form-label">权益名称</label>
                  <div class="layui-input-inline">
                    <select name="equityType" lay-filter="equityType" v-model="itemData.equityType.id">
                      <option v-for="(item,index) in lxlist" :value="item.id" :key="index">{{item.name}}</option>

                    </select>
                  </div>
                </div>

                <div class="layui-form-item" id="test1">
                  <label class="layui-form-label">优惠券名称</label>
                  <div class="layui-input-inline">
                    <select name="conditionId" lay-filter="conditionId"
                      v-model="itemData.effectiveConditions[0].conditionId">
                      <option v-for="item in yhjlist" :value="item.id">{{item.name}}</option>
                    </select>
                  </div>
                  <span class="notes">*当权益类型为赠送优惠券时选择</span>
                </div>

                <div class="layui-form-item" id="test2">
                  <div class="layui-inline">
                    <label class="layui-form-label">数量</label>
                    <div class="layui-input-inline">
                      <input type="number" name="number" placeholder="请输入数量" autocomplete="off"
                        v-model="itemData.effectiveConditions[0].number" class="layui-input" />
                    </div>
                  </div>
                  <span class="notes">*当权益类型为赠送优惠券时选择</span>
                </div>

                <div class="layui-form-item">
                  <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-normal" @click="sure">
                      确认
                    </button>

                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "memberqyadd",
  data () {
    return {
      id: "",
      itemData: {
        effectiveConditions: [{
          conditionId: "",
          number: ""
        }],
        levelId: "", //会员等级ID
        levelName: "", //会员等级
        equityType: {
          id: "",// id
          name: "",//权益名称
          describe: ""//权益描述
        },

      },
      lxlist: [],//类型
      djlist: [],//等级
      yhjlist: [],//优惠券
      type: localStorage.getItem("type")
    };
  },
  mounted () {
    var _this = this;
    this.getlx();//类型
    this.getdj();//等级
    this.getyhj();//优惠券
    window.getdates = this.getdates;
  },
  methods: {
    getdj: function () {
      var vm = this;
      var url = "/shop/aapi/v1.9/levels/"+  this.$route.query.id;
      ajaxRequest(url, "GET", "json", "", function (ret, err) {
        vm.itemData.levelId = ret.data.id
        vm.itemData.levelName = ret.data.name
      });
    },
    getlx: function () {
      var vm = this;
      var url = "/crm/papi/v1.9/membership-interests/equity/type";
      ajaxRequest(url, "GET", "json", "", function (ret, err) {
        vm.lxlist = ret.data;

      });
    },
    getyhj: function () {
      var vm = this;
      var url = "/crm/papi/v1.9/coupon/admin/template";
      ajaxRequest(url, "GET", "json", "", function (ret, err) {
        vm.yhjlist = ret.data.list;
        // alert(JSON.stringify(ret.data))
      });
    },
    sure: function (data) {
      var _this = this;
  //     if(_this.itemData.effectiveConditions[0].conditionId != null){
  // if (_this.itemData.equityType.id != "5") {
  //       layer.msg("只有赠送优惠券的权益类型可选优惠券");
  //       return false;
  //     }
  //     }
      
      this.id = data.id;
      var dates = [_this.itemData];
      // alert(JSON.stringify(dates));
      if (_this.type == "1") {
        var url = "/crm/aapi/v1.9/membership-interests";
      } else {
        var url = "/crm/bapi/v1.9/membership-interests";
      }

      ajaxRequest(url, "POST", "json", dates, function (ret, err) {
        layer.msg("操作成功");
        setTimeout(function () {
          var index = parent.layer.getFrameIndex(window.name);
          parent.layer.close(index);
          parent.location.reload();
        }, 1000);
      });
    },
  },
  updated: function () {
    var _this = this;
    layui.use("form", function () {
      var form = layui.form;
      form.render();
      form.on("select(equityType)", function (data) {
        _this.itemData.equityType.id = data.value;
        if(data.value==5){
            document.getElementById("test1").style.display="";
            document.getElementById("test2").style.display="";
        }else {
            document.getElementById("test1").style.display="none";
            document.getElementById("test2").style.display="none";
        }
      });
      form.on("select(conditionId)", function (data) {
        _this.itemData.effectiveConditions[0].conditionId = data.value;
      });

    });
  }
};
</script>
<style scoped>
.notes {
  color: #999;
}
</style>
